package com.retrofit.library.callback;

import android.support.annotation.NonNull;
import android.util.Log;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 如果 解析的结果直接返回 JSONObject,需要自定义converter(  )
 * 要么解析成字符串，要么解析成实体类
 *
 * desc:    请求回调类，
 * date:    2017/9/9/0023
 */
public abstract class RespCallback<T> implements Callback<T> {

    public RespCallback() {
        onStart();
    }

    @Override
    public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
        switch (response.code()) {
            case 200:
                T t = response.body();
                onSuccess(t);
                break;
            default:
                onError(response.code() + "",
                        "response error,detail = " + response.toString());
                break;
        }
    }

    @Override
    public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
        //网络问题会走该回调
        Log.e("RetrofitDemo",t.toString());

    }


    private void onStart() {

    }

    public void onError(String errorCode, String errorMsg) {

    }


    public abstract void onSuccess(T t);

}
